package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.BaseContext;
import com.example.demo.entity.Account;
import com.example.demo.entity.Bill;
import com.example.demo.entity.Orders;
import com.example.demo.mapper.AccountMapper;
import com.example.demo.service.AccountService;
import com.example.demo.service.BillService;
import com.example.demo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BillService billService;

    /**
     * 查询用户钱包余额
     * @return
     */
    @Override
    public BigDecimal getBalance() {
        //通过登录用户的id查询用户钱包余额
        return super.getById(BaseContext.getCurrentId()).getBalance();
    }

    /**
     * 用户消费100元
     * @param password
     */
    @Override
    public void pay(String password, Long orderId) {
        Account account = super.getById(BaseContext.getCurrentId());
        //对比支付密码
        if(!account.getPassword().equals(password)){
//            throw new Exception("密码错误");
        }

        //通过订单号获取订单金额
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getOrderId, orderId);
        Orders order = orderService.getOne(wrapper);
        BigDecimal price = order.getPrice();
        //获取余额
        BigDecimal balance = account.getBalance();

        //支付
        if(balance.compareTo(price) < 0){
//            throw new Exception("余额不足");
        }
        //更新余额
        account.setBalance(balance.subtract(price));
        super.updateById(account);

        //添加账单信息
        Bill bill = new Bill();
        bill.setAccountId(account.getId());
        bill.setOrderId(order.getOrderId());
        bill.setPm(0);
        bill.setNumber(price);
        bill.setCreateTime(LocalDateTime.now());
        billService.save(bill);
    }

    /**
     * 用户退款20元
     * @param orderId
     */
    @Override
    public void refund(Long orderId) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getOrderId, orderId);
        Orders order = orderService.getOne(wrapper);
        //获取订单状态 0-交易成功 1-申请退款 2-商家处理 3-退款中 4-退款成功
        //若商家同意退款，则退款
        if(order.getStatus() == 3){
            //获取订单金额
            BigDecimal price = order.getPrice();
            //获取余额
            Account account = super.getById(BaseContext.getCurrentId());
            BigDecimal balance = account.getBalance();

            //更新余额
            account.setBalance(balance.add(price));
            super.updateById(account);

            //修改订单状态
            order.setStatus(4);
            orderService.updateById(order);

            //添加账单信息
            Bill bill = new Bill();
            bill.setAccountId(account.getId());
            bill.setOrderId(order.getOrderId());
            bill.setPm(1);
            bill.setNumber(price);
            bill.setCreateTime(LocalDateTime.now());
            billService.save(bill);
        }
    }
}
